package com.eric.ztea.controller;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eric.ztea.dao.DepositRepository;
import com.eric.ztea.domain.Deposit;

@RestController
@RequestMapping("/api/deposit")
public class DepositController {

	@Autowired
    DepositRepository depositRepository;

	@GET
	@RequestMapping(value = "/findById")
	public Deposit findById(@RequestParam(value = "id", required = true) Long id) {
		return depositRepository.findById(id);
	}
	
	@GET
	@RequestMapping(value = "/findByUserId")
	public List<Deposit> findByUserId(@RequestParam(value = "userid", required = true) Long userid) {
		return depositRepository.findByUserid(userid);
	}
	
	@POST
	@RequestMapping(value = "/save")
	public Deposit save(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "userid", required = true) Long userid,
			@RequestParam(value = "cost", required = true) String cost,
			@RequestParam(value = "type", required = true) Long type) {
		return depositRepository.save(new Deposit(id, userid, cost, null, type));
	}
	
	@DELETE
	@RequestMapping(value = "/delete")
	public void delete(@RequestParam(value = "id", required = true) Long id)
	{
		depositRepository.delete(id);
	}
}
